package net.hserver.apm.queue;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import net.hserver.apm.common.DataConfig;
import net.hserver.apm.config.MongodbConfig;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.hserver.core.ioc.annotation.Autowired;
import cn.hserver.core.ioc.annotation.queue.QueueHandler;
import cn.hserver.core.ioc.annotation.queue.QueueListener;

/**
 * @author hxm
 */
@QueueListener(queueName = "APM_REC")
public class DataQueue {

    @Autowired
    private MongoClient mongoClient;

    private static final Logger log = LoggerFactory.getLogger(DataQueue.class);

    @QueueHandler(sizePropValue = "apm.size",size = 8)
    public void save(String type, String json) {
        MongoDatabase database = mongoClient.getDatabase(MongodbConfig.DATA_BASE);
        try {
            switch (type) {
                case DataConfig.APM_DATA_INFO:
                    MongoCollection<Document> server_info = database.getCollection("serverInfo");
                    server_info.insertOne(Document.parse(json));
                    break;
                case DataConfig.APM_DATA_REQUEST_INFO:
                    MongoCollection<Document> request_info = database.getCollection("requestInfo");
                    request_info.insertOne(Document.parse(json));
                    break;
                case DataConfig.APM_DATA_LOG_INFO:
                    MongoCollection<Document> log_info = database.getCollection("logInfo");
                    log_info.insertOne(Document.parse(json));
                    break;
                case DataConfig.APM_DATA_LINK_INFO:
                    MongoCollection<Document> link_info = database.getCollection("linkInfo");
                    link_info.insertOne(Document.parse(json));
                    break;
                case DataConfig.APM_DATA_SQL_INFO:
                    MongoCollection<Document> sqlInfo = database.getCollection("sqlInfo");
                    sqlInfo.insertOne(Document.parse(json));
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
